<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page isELIgnored="false" %>
<%
    String path = request.getContextPath();
%>
<!DOCTYPE html>
<html>
<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>李宁体育管理系统</title>

    <link rel="shortcut icon" href="favicon.ico">
    <link href="<%=path%>/css/bootstrap.min14ed.css?v=3.3.6" rel="stylesheet">
    <link href="<%=path%>/css/font-awesome.min93e3.css?v=4.4.0" rel="stylesheet">
    <link href="<%=path%>/css/style.min862f.css?v=4.1.0" rel="stylesheet">
    <link href="<%=path%>/css/plugins/chosen/chosen.css" rel="stylesheet">

    <link href="<%=path%>/css/plugins/awesome-bootstrap-checkbox/awesome-bootstrap-checkbox.css"
          rel="stylesheet">
    <link href="<%=path%>/css/animate.min.css" rel="stylesheet">
    <link href="<%=path%>/css/style.min862f.css?v=4.1.0" rel="stylesheet">
	
	<style type="text/css">
    html,body{margin:0;padding:0;}
    .iw_poi_title {color:#CC5522;font-size:14px;font-weight:bold;overflow:hidden;padding-right:13px;white-space:nowrap}
    .iw_poi_content {font:12px arial,sans-serif;overflow:visible;padding-top:4px;white-space:-moz-pre-wrap;word-wrap:break-word}
	</style>
</head>

<body class="gray-bg" ontouchstart>
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
             <div class="ibox-title">
                    <h5>门店维护</h5>

                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                        <a class="close-link">
                            <i class="fa fa-times"></i>
                        </a>
                    </div>
                </div>
                <div class="ibox-content">
             		<form role="form" id="queryForm" class="form-inline">           		
                        <div class="form-group">
                        	<input class="form-control" type="text" style="width:250px;" id="text_" name="text_"
                                   value="" placeholder="请输入查询地址"/>
                                   
                        </div>
                         <div class="input-group">
                            <button class="btn btn-primary" type="button" style="margin-bottom: 0px;"
                                     onclick="search(); "><i class="fa fa-search"></i>&nbsp;查询
                            </button>
                        </div>
                        <%-- <div class="form-group">
                        	<input class="form-control" type="text" style="width:250px;" id="latitude" name="latitude"
                                   value="${map.latitude}" placeholder="纬度" />
                        </div> --%>
                    </form>
                </div>
            </div>
        </div>
    </div>
        <div class="ibox float-e-margins">
               <div class="ibox-content">
             		<form role="form" id="storeForm" class="form-inline">
             			<input class="form-control" type="hidden" value="${map.id}" id="id" name="id"/>
             			 <div class="form-group">
                        	<input class="form-control" type="text" style="width:250px;" id="longitude" name="longitude"
                                   value="" placeholder="经度"/>
                                   
                        </div>
                        <div class="form-group">
                        	<input class="form-control" type="text" style="width:250px;" id="latitude" name="latitude"
                                   value="" placeholder="纬度"/>
                                   
                        </div>
                        <div class="form-group">
                        	<input class="form-control" type="text" style="width:250px;" id="telephone" name="telephone"
                                   value="${map.telephone}" placeholder="电话"/>
                                   
                        </div>
                         <div class="input-group">
                            <button class="btn btn-primary" type="submit" style="margin-bottom: 0px;"
                                     ><i class="fa fa-search"></i>&nbsp;保存
                            </button>
                        </div>
                        <span id='error_longitude' style='color: red'></span>
                        <span id='error_latitude' style='color: red'></span>
                        <span id='error_telephone' style='color: red'></span>
                    </form>
                </div>
        <div class="ibox-content">
            <div class="row row-lg">
                <div class="col-sm-12">
                    <!-- Example Events -->
                    <div class="example-wrap">
                       <div style="height:550px;border:#ccc solid 1px;" id="dituContent"></div> 
							
					
                </div>
                <!-- End Example Events -->
            </div>
        </div>
    </div>

    
</div>
<script src="<%=path%>/js/jquery.min.js?v=2.1.4"></script>
<!--下拉框-->
<script src="<%=path%>/js/plugins/chosen/chosen.jquery.js"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?key=ffBCmoxkXPT1B0kBYmxNhkg0fQ7BlZQo&v=1.1&services=true"></script>
<script src="<%=path%>/js/bootstrap.min.js"></script>
<script type="text/javascript" src="<%=path%>/js/plugins/layer/layer.min.js"></script>
<script type="text/javascript" src="<%=path%>/js/jquery.validate.js"></script>
<script type="text/javascript" src="<%=path%>/js/jquery.serializeObject.js"></script>
<script>
    <!-- 地图开始 -->
    $(function(){
        var config = {
                ".chosen-select": {},
                ".chosen-select-deselect": {allow_single_deselect: !0},
                ".chosen-select-no-single": {disable_search_threshold: 10},
                ".chosen-select-no-results": {no_results_text: "Oops, nothing found!"},
                ".chosen-select-width": {width: "95%"}
            };
        for (var selector in config)$(selector).chosen(config[selector]);
        initMap();//创建和初始化地图
    }); 

    
  //创建和初始化地图函数：
    function initMap(){
        createMap();//创建地图
        setMapEvent();//设置地图事件
        addMapControl();//向地图添加控件
        addMarker();//向地图中添加marker
    }
    
    //创建地图函数：
    function createMap(){
        var map = new BMap.Map("dituContent");//在百度地图容器中创建一个地图
        var point=null;
        if(""!=$("#text_").val()){
        	var text =$("#text_").val();
        	 Geocoder(text);
        }else{
        	
        		 point = new BMap.Point(<c:choose><c:when test="${not empty map.longitude }">${map.longitude}</c:when><c:otherwise>121.483634</c:otherwise></c:choose>,
        				 <c:choose><c:when test="${not empty map.latitude }">${map.latitude}</c:when><c:otherwise>31.241393</c:otherwise></c:choose>);//定义一个中心点坐标
        
        	
           
            $("#longitude").val(point.lng);
        	$("#latitude").val(point.lat);
            map.centerAndZoom(point,14);//设定地图的中心点和坐标并将地图显示在地图容器中
        }
        window.map = map;//将map变量存储在全局
    }
    
    function Geocoder(endAddress) {
        var myGeo = new BMap.Geocoder();
        // 将地址解析结果显示在地图上,并调整地图视野
        myGeo.getPoint(endAddress, function(point){
            if (point) {
            	$("#longitude").val(point.lng);
            	$("#latitude").val(point.lat);
                map.centerAndZoom(point, 16);
                map.addOverlay(new BMap.Marker(point));                
            }else{
                alert("您选择地址没有解析到结果!");
            }
        }, "上海市");
    }; 
    
    //地图事件设置函数：
    function setMapEvent(){
        map.enableDragging();//启用地图拖拽事件，默认启用(可不写)
        map.enableScrollWheelZoom();//启用地图滚轮放大缩小
        map.enableDoubleClickZoom();//启用鼠标双击放大，默认启用(可不写)
        map.enableKeyboard();//启用键盘上下左右键移动地图
    }
    
    //地图控件添加函数：
    function addMapControl(){
        //向地图中添加缩放控件
	var ctrl_nav = new BMap.NavigationControl({anchor:BMAP_ANCHOR_TOP_LEFT,type:BMAP_NAVIGATION_CONTROL_LARGE});
	map.addControl(ctrl_nav);
        //向地图中添加缩略图控件
	var ctrl_ove = new BMap.OverviewMapControl({anchor:BMAP_ANCHOR_BOTTOM_RIGHT,isOpen:1});
	map.addControl(ctrl_ove);
        //向地图中添加比例尺控件
	var ctrl_sca = new BMap.ScaleControl({anchor:BMAP_ANCHOR_BOTTOM_LEFT});
	map.addControl(ctrl_sca);
    }
    
    //标注点数组
    var markerArr = [{title:"${map.name}",content:"李宁篮球",point:"${map.longitude}|${map.latitude}",isOpen:0,icon:{w:21,h:21,l:0,t:0,x:6,lb:5}}
		 ];
    //创建marker
    function addMarker(){
        for(var i=0;i<markerArr.length;i++){
            var json = markerArr[i];
            var p0 = json.point.split("|")[0];
            var p1 = json.point.split("|")[1];
            var point = new BMap.Point(p0,p1);
			var iconImg = createIcon(json.icon);
            var marker = new BMap.Marker(point,{icon:iconImg});
			var iw = createInfoWindow(i);
			var label = new BMap.Label(json.title,{"offset":new BMap.Size(json.icon.lb-json.icon.x+10,-20)});
			marker.setLabel(label);
            map.addOverlay(marker);
            label.setStyle({
                        borderColor:"#808080",
                        color:"#333",
                        cursor:"pointer"
            });
         
			(function(){
				var index = i;
				var _iw = createInfoWindow(i);
				var _marker = marker;
				_marker.addEventListener("click",function(){
				    this.openInfoWindow(_iw);
			    });
			    _iw.addEventListener("open",function(){
				    _marker.getLabel().hide();
			    })
			    _iw.addEventListener("close",function(){
				    _marker.getLabel().show();
			    })
				label.addEventListener("click",function(){
				    _marker.openInfoWindow(_iw);
			    })
				if(!!json.isOpen){
					label.hide();
					_marker.openInfoWindow(_iw);
				}
			})()
        }
    }
    //创建InfoWindow
    function createInfoWindow(i){
        var json = markerArr[i];
        var p0 = json.point.split("|")[0];
        var p1 = json.point.split("|")[1];
        var content = "<form role='form' id='queryForm' class='form-inline' width='352' height='600'>"+
    	"<div class='c2'>"+
        "<div class='n_p_lineheight'>"+
        "<input class='form-control' type='hidden'  id='id' name='id'  value='"+1+"'/>"+
        "<span class='inline_prop'>联系人：${map.contact}</span>&nbsp;&nbsp;"+
        "</div>"+
        "<div class='n_p_lineheight'>"+
        "<span>电&nbsp;话：</span>"+
        "<input class='form-control' type='text'  id='telephone' name='telephone'  value=' ${map.telephone}' placeholder='联系电话' disabled/>"+
        "<span id='error_telephone' style='color: red'>*</span>"+
        "</div>"+
        "<div class='n_p_lineheight'>"+
        "<span>经&nbsp;度：</span>"+
        "<input class='form-control' type='text'  id='longitude' name='longitude'  value='"+p0+"' placeholder='经度' disabled/>"+
        "<span id='error_longitude' style='color: red'>*</span>"+
        "</div>"+
        "<div class='n_p_lineheight'>"+
        "<span>纬&nbsp;度：</span>"+
        "<input class='form-control' type='text'  id='latitude' name='latitude'  value='"+p1+"' placeholder='纬度' disabled/>"+
        "<span id='error_latitude' style='color: red'>*</span>"+
        "</div>"+
        "<div class='n_p_lineheight'>"+
        "<span>地&nbsp;&nbsp;&nbsp;址: ${map.address} </span>"+
        "</div>"+
        "<div class='n_p_lineheight'>"+
        //"<button class='btn btn-primary' type='submit'>提交</button>"+
      	/* "&nbsp;&nbsp;&nbsp;&nbsp;<button class='btn btn-success' type='button' onclick='addRemark1()'>定位</button>"+ */
        "</div>"+
        "</div>"+
        "</form>"
        ;
       // var iw = new BMap.InfoWindow("<b class='iw_poi_title' title='" + json.title + "'>" + json.title + "</b><div class='iw_poi_content'>"+json.content+"</div>");
		 var opts = {
		
		      title: "${map.name}",
		
		      maxWidth: 600,
		
		      height: 250
		}
        var iw = new BMap.InfoWindow(content,opts);
        return iw;
    }
    //创建一个Icon
    function createIcon(json){
        var icon = new BMap.Icon("<%=path%>/img/aa.png", new BMap.Size(json.w,json.h),{imageOffset: new BMap.Size(-json.l,-json.t),infoWindowOffset:new BMap.Size(json.lb+5,1),offset:new BMap.Size(json.x,json.h)})
        return icon;
    }
 //文字标注数组
    

    
 
   function search() {
	   initMap();//创建和初始化地图
    }
    
    $("#storeForm").validate({
	    onkeyup: false,
		rules: {
			telephone: {
				rangelength:[11,15],
				required: true, 				
			},
			longitude: {
				number:true,
				required: true,
			},
			latitude: {
				number:true,
				required: true,
			},
		},
		messages:{
			telephone: {
				required: "手机号不能为空",
				rangelength:"输入的长度不符合",
			},
			longitude: {
				required: "经度不能为空",
				number: "输入有效参数.",
			},
			latitude: {
				required: "纬度不能为空",
				number: "输入有效参数.",
			},

		},
		errorPlacement: function(error, element) {
			$( element ).closest( "form" ).find( "span[id='error_" + element.attr( "name" ) + "']" ).append( error );
		},
		submitHandler:function(){	
			var archiveForm = $("#storeForm").serializeObject();
			jQuery.ajax({
				type: "post",
			    url: "${pageContext.request.contextPath}/store/toUpdateStoreInfo",
			    contentType: "application/json",
			    data: JSON.stringify(archiveForm),
			    beforeSend:function() {
			    	$("#progress").modal();
			    	$("#loading").html("正在提交档案数据，请稍候...");

			    },
			    complete:function(data) {
			    	$("#progress").modal('hide');
			    	$("#loading").html("");
			    }, 
			    success: function (node) {
			    	if(node.errorCode==0){
			    		parent.layer.msg(node.errorMsg,{time: 500},function(){goback();});
			    	}else{
			    		parent.layer.msg(node.errorMsg);		    		
			    	}
			    },
			    error: function () {
			    	parent.layer.msg("保存档案数据信息失败,请联系管理员!");	
			    }	
			});	
		}
	});  
    function goback() {
    	window.location.href="<%=path%>/store/listStore";
	}

</script>
</body>
</html>
